<?php

namespace App\Service\WorkOrder\Permission;

use App\Enums\WorkOrder\OrderEnum;
use App\Http\Middleware\OpenShop\UserPermissionAuth;
use App\Models\BaseModel;
use App\Models\WorkOrder\WorkOrderApplyMainModel;
use App\Models\WorkOrder\WorkOrderUserRoleModel;

class PermissionService
{
    public const URI_WORK_ORDER_LISTS = 'workorder.web.orderlists';

    /**
     * 权限通用返回值
     * @param bool $isAll
     * @param string $rawSql
     * @return array
     */
    private function permissionData(bool $isAll, string $rawSql = ''): array
    {
        return [
            'isAll'  => $isAll,
            'rawSql' => $rawSql,
        ];
    }

    /**
     * 订单列表权限
     * @param int $userId
     * @return array
     */
    public function orderLists(int $userId): array
    {
        $uri            = self::URI_WORK_ORDER_LISTS;
        $permissionList = getRmsUserPermissionList([], UserPermissionAuth::MODULE_WORK_ORDER);

        // 判断是否拥有权限
        if (!in_array($uri, $permissionList)) {
            return $this->permissionData(false, '1=0');
        }

        // 判断是否拥有所有权限
        if (in_array($uri . '_all', $permissionList)) {
            return $this->permissionData(true);
        }

        // 用户角色
        $roles = WorkOrderUserRoleModel::query()->where('userId', $userId)->select('role', 'problemPid', 'problemChildId')->get();

        // 客服角色 可以看到所有类型的工单
        if ($roles->where('role', WorkOrderUserRoleModel::ROLE_TYPE_1)->count()) {
            return $this->permissionData(true);
        }

        // + 我处理过的
        $builder = WorkOrderApplyMainModel::query()
                                          ->join('work_order_missions as mission', 'mission.workOrderNo', '=',
                                              'work_order_apply_main.workOrderNo')
            ->where('mission.isDeleted', 0)
                                          ->where(function ($query) use ($userId, $roles) {
                                              $query->where('mission.dealUid', $userId);

                                              foreach ($roles as $roleV) {

                                                  if ($roleV->problemPid == 0) {
                                                      continue;
                                                  }

                                                  // 其他负责人，看对应问题的订单
                                                  $query->orWhere(function ($q) use ($roleV) {
                                                      $q->where('problemPid', $roleV->problemPid);
                                                      if ($roleV->problemChildId) {
                                                          $q->where('problemChildId', $roleV->problemChildId);
                                                      }
                                                  });
                                              }
                                          })
                                          ->select('work_order_apply_main.workOrderNo');

        return $this->permissionData(false, "workOrderNo in (" . BaseModel::toSqlWithParam($builder) . ')');
    }
}
